package com.ruoyi.web.service.impl;

import java.util.HashMap;
import java.util.List;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.mapper.OtherNewsMapper;
import com.ruoyi.web.domain.OtherNews;
import com.ruoyi.web.service.IOtherNewsService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 其他新闻类Service业务层处理
 * 
 * @author drx
 * @date 2024-03-20
 */
@Service
public class OtherNewsServiceImpl implements IOtherNewsService 
{
    @Autowired
    private OtherNewsMapper otherNewsMapper;

    /**
     * 查询其他新闻类
     * 
     * @param id 其他新闻类主键
     * @return 其他新闻类
     */
    @Override
    public OtherNews selectOtherNewsById(Long id)
    {
        return otherNewsMapper.selectOtherNewsById(id);
    }

    /**
     * 查询其他新闻类列表
     * 
     * @param otherNews 其他新闻类
     * @return 其他新闻类
     */
    @Override
    public List<OtherNews> selectOtherNewsList(OtherNews otherNews)
    {
        return otherNewsMapper.selectOtherNewsList(otherNews);
    }

    /**
     * 新增其他新闻类
     * 
     * @param otherNews 其他新闻类
     * @return 结果
     */
    @Override
    public int insertOtherNews(OtherNews otherNews)
    {
        otherNews.setCreateTime(DateUtils.getNowDate());
        otherNews.setCreateBy(ShiroUtils.getSysUser().getLoginName());
        otherNews.setCreateName(ShiroUtils.getSysUser().getUserName());
        otherNews.setUpdateTime(DateUtils.getNowDate());
        otherNews.setUpdateBy(ShiroUtils.getSysUser().getLoginName());
        otherNews.setUpdateName(ShiroUtils.getSysUser().getUserName());
        return otherNewsMapper.insertOtherNews(otherNews);
    }

    /**
     * 修改其他新闻类
     * 
     * @param otherNews 其他新闻类
     * @return 结果
     */
    @Override
    public int updateOtherNews(OtherNews otherNews)
    {
        otherNews.setUpdateTime(DateUtils.getNowDate());
        otherNews.setUpdateBy(ShiroUtils.getSysUser().getLoginName());
        otherNews.setUpdateName(ShiroUtils.getSysUser().getUserName());
        return otherNewsMapper.updateOtherNews(otherNews);
    }

    /**
     * 批量删除其他新闻类
     * 
     * @param ids 需要删除的其他新闻类主键
     * @return 结果
     */
    @Override
    public int deleteOtherNewsByIds(String ids)
    {
        return otherNewsMapper.deleteOtherNewsByIds(Convert.toStrArray(ids),ShiroUtils.getSysUser().getLoginName());
    }

    /**
     * 删除其他新闻类信息
     * 
     * @param id 其他新闻类主键
     * @return 结果
     */
    @Override
    public int deleteOtherNewsById(Long id)
    {
        return otherNewsMapper.deleteOtherNewsById(id,ShiroUtils.getSysUser().getLoginName());
    }

    /**
     * 修改otherNews发布状态
     *
     * @param otherNews
     * @return 结果
     */
    @Transactional
    public int updateOtherNewsState(OtherNews otherNews)
    {
        otherNews.setUpdateTime(DateUtils.getNowDate());
        otherNews.setUpdateBy(ShiroUtils.getSysUser().getLoginName());
        otherNews.setUpdateName(ShiroUtils.getSysUser().getUserName());
        return otherNewsMapper.updateOtherNews(otherNews);
    }

    /**
     * 前端查询详情用
     *
     * @param id
     * @return AjaxResult
     */
    @Override
    public AjaxResult getOtherNewsById(Long id)
    {
        AjaxResult ajaxResult=new AjaxResult();
        try{
            OtherNews otherNews = otherNewsMapper.selectOtherNewsById(id);
            if(null != otherNews){
                //状态为未启用时，返回空对象
                if("0".equals(otherNews.getState())){
                    ajaxResult.put("data",null);
                    ajaxResult.put("code","1");
                    ajaxResult.put("msg","查询成功！");
                    return  ajaxResult;
                }
            }
            ajaxResult.put("data",otherNews);
            ajaxResult.put("code","1");
            ajaxResult.put("msg","查询成功！");
        } catch (Exception e){
            e.printStackTrace();
            ajaxResult.put("code","500");
            ajaxResult.put("msg","获取新闻详情失败，请联系管理员！");
        }
        return ajaxResult;
    }

    /**
     * 获取每日药闻视图
     *
     * @return 其他新闻类集合
     */
    public AjaxResult getNewsDayView(){
        AjaxResult ajaxResult=new AjaxResult();
        try{
            List<OtherNews> list = otherNewsMapper.getNewsDayView();
            ajaxResult.put("data",list);
            ajaxResult.put("code", Constants.SUCCESS);
            ajaxResult.put("msg","查询成功！");
        } catch (Exception e){
            e.printStackTrace();
            ajaxResult.put("code",Constants.FAIL);
            ajaxResult.put("msg","获取首页每日药闻失败，请联系管理员！");
        }
        return ajaxResult;
    }

    /**
     * 获取 首页-医药前沿，临床招募，远程问诊，健康护理
     *
     * @return 其他新闻类集合
     */
    public AjaxResult getOtherNewsByTypeView(){
        AjaxResult ajaxResult=new AjaxResult();
        try{
            List<OtherNews> list = otherNewsMapper.getOtherNewsByTypeView();
            ajaxResult.put("data",list);
            ajaxResult.put("code", Constants.SUCCESS);
            ajaxResult.put("msg","查询成功！");
        } catch (Exception e){
            e.printStackTrace();
            ajaxResult.put("code",Constants.FAIL);
            ajaxResult.put("msg","获取首页其他药闻失败，请联系管理员！");
        }
        return ajaxResult;
    }

    /**
     * 获取 首页-安全用药
     *
     * @return 其他新闻类集合
     */
    public AjaxResult getOtherNewsAQYY(){
        AjaxResult ajaxResult=new AjaxResult();
        try{
            List<OtherNews> list = otherNewsMapper.getOtherNewsAQYY();
            ajaxResult.put("data",list);
            ajaxResult.put("code", Constants.SUCCESS);
            ajaxResult.put("msg","查询成功！");
        } catch (Exception e){
            e.printStackTrace();
            ajaxResult.put("code",Constants.FAIL);
            ajaxResult.put("msg","获取首页安全用药失败，请联系管理员！");
        }
        return ajaxResult;
    }

    /*
     * 根据id查询上一篇下一篇
     * */
    @Override
    public AjaxResult getUpandDown(Long id){
        AjaxResult ajaxResult = new AjaxResult();
        try{
            List<HashMap<String, Object>> upandDown = otherNewsMapper.getUpandDown(id);
            ajaxResult.put("data",upandDown);
            ajaxResult.put("code","200");
            ajaxResult.put("msg","查询成功！");
        }catch (Exception e){
            e.printStackTrace();
            ajaxResult.put("code","500");
            ajaxResult.put("msg","获取新闻上下篇失败，请联系管理员！");
        }
        return ajaxResult;
    }

    /*
     * 增加新闻浏览量
     * */
    public void updateotherNewsBrowseNum(Long id){
        otherNewsMapper.updateotherNewsBrowseNum(id);
    }

}
